package com.logos.musicframe.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.logos.musicframe.constant.RedisConstant;
import com.logos.musicframe.entity.Songs;
import com.logos.musicframe.req.SongsReq;
import com.logos.musicframe.resp.SongsResp;
import com.logos.musicframe.result.Result;
import com.logos.musicframe.service.SongsService;
import com.logos.musicframe.utils.RedisUtil;
import com.logos.musicframe.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author logos
 * date 2025/5/29 18:06
 * @version v1.0
 * @Package com.logos.musicframe.controller
 */
@Controller
@Slf4j
public class SongsController {

    @Autowired
    private SongsService songsService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfoUtil userInfoUtil;

    public String[] getAllSongNames() {
        List<SongsResp> songsRespList = songsService.queryAll(userInfoUtil.getUserId());
        List<String> songNames = new ArrayList<>();
        for (SongsResp songsResp : songsRespList) {
            // 拼接歌名-歌手名
            songNames.add(songsResp.getTitle() + "-" + songsResp.getArtist());
        }
        return songNames.toArray(new String[songNames.size()]);
    }

    public Songs getSongPathByName(String songName) {
        // 分割出歌名和歌手名
        String[] split = songName.split("-");
        Songs condition = new Songs();
        condition.setTitle(split[0]);
        condition.setArtist(split[1]);
        songsService.queryByCondition(condition);
        return null;
    }

    /**
     * 上传音乐
     *
     * @return
     */
    public Result<Boolean> uploadSong(SongsReq songsReq) {
        log.info("SongsController.uploadSong.songsReq：{}" + songsReq);
        Preconditions.checkNotNull(songsReq.getTitle(), "歌曲名称不能为空");
        Preconditions.checkNotNull(songsReq.getArtist(), "歌手名称不能为空");
        Preconditions.checkNotNull(songsReq.getSongFile(), "音乐文件不能为空");
        Preconditions.checkNotNull(songsReq.getPicture(), "音乐图片不能为空");
        Boolean result = songsService.uploadSong(songsReq);
        return Result.ok(result);
    }

    /**
     * 今日推荐歌曲
     */
    public Result<List<SongsResp>> getDaySongs() {
        // 从redis中取出今日推荐歌曲
        String key = RedisConstant.SONGS_DAY + LocalDate.now();
        String songsDayStr = redisUtil.get(key);
        List<SongsResp> songsRespList = JSON.parseArray(songsDayStr, SongsResp.class);
        return Result.ok(songsRespList);
    }

    /**
     * 获取推荐歌曲
     * 基于用户喜欢和随机行为进行推荐
     */
    public Result<List<SongsResp>> getRecommendSongs() {
        try {
            // 获取所有歌曲
            List<SongsResp> allSongs = songsService.queryAll(userInfoUtil.getUserId());
            // TODO 这里修改根据歌曲喜欢和随机行为进行返回
            return Result.ok(allSongs);
        } catch (Exception e) {
            log.error("获取推荐歌曲失败", e);
            return Result.fail("获取推荐歌曲失败");
        }
    }

    /**
     * 获取所有歌曲方法
     * @return
     */
    public Result<List<SongsResp>> queryAll() {
        try {
            List<SongsResp> songsRespList = songsService.queryAll(userInfoUtil.getUserId());
            return Result.ok(songsRespList);
        } catch (Exception e) {
            log.error("获取所有歌曲失败", e);
            return Result.fail("获取所有歌曲失败");
        }
    }

    /**
     * 根据歌曲路径查询歌曲信息
     * @param filePath
     * @return
     */
    public Result<SongsResp> querySongByPath(String filePath) {
        try {
            log.info("SongsController.querySongByPath.filePath:{}",filePath);
            Songs condition = new Songs().setFilePath(filePath);
            List<SongsResp> songsRespList = songsService.queryByCondition(condition);
            if (songsRespList == null || songsRespList.isEmpty()) {
                return Result.fail("歌曲不存在");
            }
            return Result.ok(songsRespList.get(0));
        }catch (Exception e){
            log.error("SongsController.querySongByPath.error", e.getMessage(),e);
            return Result.fail("获取歌曲信息失败");
        }

    }
}
